import { ElNotification, ElMessage, ElMessageBox } from 'element-plus'
import type { NotificationOptions, MessageOptions } from 'element-plus'

export interface NotificationConfig extends Partial<NotificationOptions> {
  title?: string
  message: string
  type?: 'success' | 'warning' | 'info' | 'error'
  duration?: number
  showClose?: boolean
}

export interface MessageConfig extends Partial<MessageOptions> {
  message: string
  type?: 'success' | 'warning' | 'info' | 'error'
  duration?: number
  showClose?: boolean
}

export function useNotification() {
  
  const notify = (config: NotificationConfig) => {
    return ElNotification({
      ...config,
      title: config.title || getDefaultTitle(config.type),
      message: config.message,
      type: config.type || 'info',
      duration: config.duration ?? (config.type === 'error' ? 0 : 4500),
      showClose: config.showClose ?? true
    })
  }

  const message = (config: MessageConfig) => {
    return ElMessage({
      ...config,
      message: config.message,
      type: config.type || 'info',
      duration: config.duration ?? 3000,
      showClose: config.showClose ?? false
    })
  }

  const success = (message: string, title?: string) => {
    return notify({
      type: 'success',
      title: title || '成功',
      message
    })
  }

  const warning = (message: string, title?: string) => {
    return notify({
      type: 'warning',
      title: title || '警告',
      message
    })
  }

  const error = (message: string, title?: string) => {
    return notify({
      type: 'error',
      title: title || '错误',
      message,
      duration: 0 // 错误通知不自动关闭
    })
  }

  const info = (message: string, title?: string) => {
    return notify({
      type: 'info',
      title: title || '信息',
      message
    })
  }

  const confirm = async (
    message: string, 
    title: string = '确认操作',
    options: {
      confirmButtonText?: string
      cancelButtonText?: string
      type?: 'warning' | 'info' | 'success' | 'error'
    } = {}
  ): Promise<boolean> => {
    try {
      await ElMessageBox.confirm(message, title, {
        confirmButtonText: options.confirmButtonText || '确定',
        cancelButtonText: options.cancelButtonText || '取消',
        type: options.type || 'warning'
      })
      return true
    } catch {
      return false
    }
  }

  const prompt = async (
    message: string,
    title: string = '输入信息',
    options: {
      confirmButtonText?: string
      cancelButtonText?: string
      inputPattern?: RegExp
      inputErrorMessage?: string
      inputPlaceholder?: string
      inputValue?: string
    } = {}
  ): Promise<string | null> => {
    try {
      const { value } = await ElMessageBox.prompt(message, title, {
        confirmButtonText: options.confirmButtonText || '确定',
        cancelButtonText: options.cancelButtonText || '取消',
        inputPattern: options.inputPattern,
        inputErrorMessage: options.inputErrorMessage,
        inputPlaceholder: options.inputPlaceholder,
        inputValue: options.inputValue
      })
      return value
    } catch {
      return null
    }
  }

  const loading = (message: string = '加载中...') => {
    return ElMessage({
      message,
      type: 'info',
      duration: 0,
      showClose: false,
      customClass: 'loading-message'
    })
  }

  return {
    notify,
    message,
    success,
    warning,
    error,
    info,
    confirm,
    prompt,
    loading
  }
}

function getDefaultTitle(type?: string): string {
  const titles = {
    success: '成功',
    warning: '警告',
    error: '错误',
    info: '信息'
  }
  return titles[type as keyof typeof titles] || '通知'
}